package com.yun.many.auth.dao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.many.auth.dao.entity.User;
import com.yun.many.auth.dao.enums.SexEnum;
import com.yun.many.auth.dao.enums.UserStatusEnum;
import com.yun.many.auth.dao.mapper.UserMapper;
import com.yun.many.auth.dao.service.IUserRepository;
import com.yun.many.auth.model.req.UserQueryReq;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UserRepository extends ServiceImpl<UserMapper, User> implements IUserRepository {
    private final UserMapper userMapper;

    @Override
    public Page<User> pageUser(UserQueryReq req) {
        return new LambdaQueryChainWrapper<>(userMapper)
                .eq(StringUtils.isNotBlank(req.getUsername()), User::getUsername, req.getUsername())
                .eq(req.getSex() != null, User::getSex, SexEnum.of(req.getSex()))
                .eq(req.getStatus() != null, User::getStatus, UserStatusEnum.of(req.getStatus()))
                .page(req);
    }

    @Override
    public User getByUsername(String username) {
        List<User> users = new LambdaQueryChainWrapper<>(userMapper)
                .eq(User::getUsername, username)
                .list();
        if (CollectionUtils.isEmpty(users)) {
            return null;
        }
        return users.get(0);
    }

    @Override
    public Boolean delete(List<Long> ids) {
        return userMapper.deleteByIds(ids) > 0;
    }
}
